<?php

class InformController
{
    private $informModel;
    private $autoUidModel;
    private $reportModel;
    private $diaryListModel;
    private $topicCommentsModel;
    private $articleListModel;
    private $anonymousModel;
    private $topicModel;
    private $groupInfoModel;
    private $rUid;
    private $bodyId;
    private $childCommentModel;

    function __construct()
    {
        $this->informModel          =   new InformModel();
        $this->autoUidModel         =   new AutoUidModel();
        $this->reportModel          =   new ReportModel();
        $this->diaryListModel       =   new DiaryListModel();
        $this->topicCommentsModel   =   new TopicCommentsModel();
        $this->articleListModel     =   new ArticleListModel();
        $this->anonymousModel       =   new AnonymousModel();
        $this->topicModel           =   new TopicModel();
        $this->groupInfoModel       =   new GroupInfoModel();
        $this->childCommentModel    =   new ChildCommentModel();
    }

    public function postInform($info)/*{{{*/
    {
        if (empty($info['informedUid']) ||  empty($info['informObject']) || empty($info['informType']))
        {
            AC::$errorNo    =   'IC001';
            return  FALSE;
        }

        //匿名举报处理
        if ($info['informObject'] == 7){
            $bodyId = (int)$info['bodyId'];
            if ( ($bodyId % 1000) >31){
                AC::$errorNo    =   'IC001';
                return  FALSE;
            }
            $anonymous = new AnonymousModel();
            $anony = $anonymous->getAnonymousInfoByBodyId($bodyId);
            if (AC::$errorNo){
                return false;
            }
            $info['informedUid'] = $anony['uid'];
            $info['informReason'] = '[举报匿名:bodyid-'.$bodyId.']'.$info['informReason'];
        }

        $informedUserInfo   =   $this->autoUidModel->getUserBaseInfo(array('uid' => $info['informedUid']));
        if (empty($informedUserInfo['account']) ||  empty($informedUserInfo['nickname']))
        {
            AC::$errorNo    =   'IC002';
            return  FALSE;
        }
        
        // 官方账号不能被举报
        if ($info['informedUid'] < 20){
            AC::$errorNo    =   'IC003';
            AC::$errorMsg   =   '抱歉，'.$informedUserInfo['nickname'].'不能被举报';
            return  FALSE;
        }

        $info['informUid']          =   AC::$hostUserBaseInfo['uid'];
        $info['informNickname']     =   AC::$hostUserBaseInfo['nickname'];
        $info['informedNickname']   =   $informedUserInfo['nickname'];
        // 圈子举报,如果举报的是评论则去获取评论所在的表
        if (!empty($info['bodyId'])){
            $tmp  = explode(',',$info['bodyId']);
            // 举报话题则将gid过滤
            if ( count($tmp) == 2 && $info['informObject'] == '4'){
                $info['bodyId'] = $tmp[0];
                $topicModel = new TopicModel();
                $tid = $tmp[0];
                $topicInfo  = $topicModel->getTopicInfo(array('tid'=>$tid));
                if (empty($topicInfo)){
                	AC::$errorNo    =   'TPM003';
                	return  FALSE;
                }
            }
            // 举报评论
            if (count($tmp) > 2){
                $topicModel = new TopicModel();
                $tid = $tmp[1];
                $topicInfo  = $topicModel->getTopicInfo(array('tid'=>$tid));
                if (empty($topicInfo)){
                	AC::$errorNo    =   'TPM003';
                	return  FALSE;
                }
                $client     = AC::getDbClient('TPC/'.$topicInfo['uid']);
                $info['bodyId'] =  $tmp[0].$client['tableSuffix'];
            }
        }
        
        $result =   $this->informModel->postInform($info);
        return  $result;
    }/*}}}*/

    public function updateInform($info)/*{{{*/
    {
        $info['editorUid']      =   AC::$hostUserBaseInfo['uid'];
        $info['editorNickname'] =   AC::$hostUserBaseInfo['nickname'];

        $result =   $this->informModel->updateInform($info);
        return  $result;
    }/*}}}*/

    public function removeInform($info)/*{{{*/
    {
        $result =   $this->informModel->removeInform($info);
        return  $result;
    }/*}}}*/

    public function getInformList($info)/*{{{*/
    {
        $result =   $this->informModel->getInformList($info);
        return  $result;
    }/*}}}*/

    public function postReport($param){
        $uid            = isset($param['uid'])          ? intval($param['uid']) : 0;
        $rUid           = isset($param['rUid'])         ? intval($param['rUid']) : 0;
        $reasonType     = isset($param['reasonType'])   ? intval($param['reasonType']) : 0;
        $type           = isset($param['type'])         ? intval($param['type']) : 0;
        if($uid < 1 || $reasonType < 1 || $type < 1) {
            AC::$errorNo    =   'IC001';
            return  FALSE;
        }
        //校验举报人信息
        $userInfo   =   $this->autoUidModel->getUserBaseInfo(array('uid' => $uid));
        if (empty($userInfo['account']) ||  empty($userInfo['nickname']))
        {
            AC::$errorNo    =   'IC002';
            return  FALSE;
        }
        $param['nickname']      =   $userInfo['nickname'];
        if($param['nickname'] == '粉粉用户') {
            $param['nickname'] = $param['nickname'] . $uid;
        }

        if (!isset($param['content']) || empty($param['content'])){
            AC::$errorNo    =   'IC004';
            AC::$errorMsg   =   '举报内容不能为空';
            return  FALSE;
        }

        //校验举报内容
        $result =  $this->checkReportInfo($param, $type);

        if(!$result) {
            return FALSE;
        }else{
            $param['content'] = $result;
        }
        //匿名加上被举报uid
        if(in_array($type, [5,6])) {
            $rUid = $this->rUid;
            $param['reasonRemark'] = '[举报匿名:bodyid-'.$this->bodyId.']'.$param['reasonRemark'];
        }
        //校验被举报人信息
        $rUserInfo   =   $this->autoUidModel->getUserBaseInfo(array('uid' => $rUid));
        if(empty($rUserInfo['account']) ||  empty($rUserInfo['nickname'])){
            AC::$errorNo    =   'IC002';
            return  FALSE;
        }
        // 官方账号不能被举报
        if ($rUid < 20){
            AC::$errorNo    =   'IC003';
            AC::$errorMsg   =   '抱歉，'.$rUserInfo['nickname'].'不能被举报';
            return  FALSE;
        }
        $param['rUid']     =   $rUid;
        $param['rnickname']     =   isset($rUserInfo['nickname']) ? $rUserInfo['nickname'] : '';
        if($param['rnickname'] == '粉粉用户') {
            $param['rnickname'] = $param['rnickname'] . $rUid;
        }
        //不能举报自己
        if($rUid == $uid || $rUid == AC::HOST_UID) {          //匿名、匿名评论没有rUid
            AC::$errorNo    =   'IC007';
            AC::$errorMsg   =   '抱歉，不能举报自己';
            return  FALSE;
        }

        $result = $this->reportModel->createReportInfo($param);
        if($result) {
            return ['reportStatus'  =>  true];
        } else {
            return ['reportStatus'  =>  false];
        }
    }

    /**
     * 举报内容校验
     * @param $param
     * @param $type
     * @return bool
     */
    public function checkReportInfo($param, $type) {
        $content = $param['content'];
        if(empty($type)){
            AC::$errorNo = 'IC006';
            AC::$errorMsg = '举报内容类型错误';
            return FALSE;
        }
        //验证内容是否重复
        if($type != 1) {
            if(empty($param['action'])) {
                AC::$errorNo    =   'IC009';        //action   不能为空（私信除外）
                return  FALSE;
            }
            $result = $this->reportModel->getReportListByUid($param['uid'], $param['rUid'], $param['type'], $param['action']);
            if($result) {
                AC::$errorNo    =   'IC008';
                if($type == 9) {
                    AC::$errorMsg   =   '您已举报过该用户，请耐心等待处理结果';
                } else {
                    AC::$errorMsg   =   '您已举报过该内容，请耐心等待处理结果';
                }
                return  FALSE;
            }
            $result = $this->reportModel->getReportListByType($param['rUid'], $param['type'], $param['action']);
            if($result) {
                AC::$errorNo    =   'IC008';
                if($type == 9) {
                    AC::$errorMsg   =   '该用户已被举报且已被处理';
                } else {
                    AC::$errorMsg   =   '该内容已被举报且已被处理';
                }
                return  FALSE;
            }
        }

        switch ($type) {
            case 1:         //私信
                $message = isset($content['message']) ? $content['message'] : array();
                if(empty($message)) {
                    AC::$errorNo    =   'IC004';
                    AC::$errorMsg   =   '举报内容不能为空';
                    return  FALSE;
                }
                foreach ($content['message'] as $key => $value){
                    if(strlen($value['summary'])< 1){
                        AC::$errorNo    =   'IC004';
                        AC::$errorMsg   =   '举报内容不能为空';
                        return  FALSE;
                    }
                    if($value['type'] != 1 && strlen($value['path']) < 1){
                        AC::$errorNo    =   'IC004';
                        AC::$errorMsg   =   '举报内容不能为空';
                        return  FALSE;
                    }
                    $content['message'][$key]['summary'] = UK::getstr($value['summary'], 1000, '' , 1);
                }

                break;
            case 2:         //点滴
                $diary = $content['diary'];
                $content['diary']['summary'] = UK::getstr($content['diary']['summary'], 1000, '' , 1);
            case 3:         //评论
                $diary = isset($diary) ? $diary : $content['diaryComment'];
                $diaryInfo = $this->diaryListModel->getDiaryInfoByBodyId($diary['bodyId']);
                if(empty($diaryInfo)){
                    AC::$errorNo    =   'IC005';
                    AC::$errorMsg   =   '内容不存在或已被删除';
                    return  FALSE;
                }
                if(isset($content['diaryComment'])) {
                    //验证评论是否存在
                    if(isset($diary['positionId']) && !empty($diary['positionId'])){
                        // 楼中楼~
                    }else{
                        $info = [
                            'position'  =>  $diary['position'],
                            'tid'       =>  $diary['bodyId'],
                            'author_uid'=>  $diary['authorUid'],
                        ];
                        $commentInfo = $this->topicCommentsModel->getCommentUid($info);
                        if(empty($commentInfo)){
                            AC::$errorNo    =   'IC005';
                            AC::$errorMsg   =   '内容不存在或已被删除';
                            return  FALSE;
                        }
                    }
                    $content['diaryComment']['summary'] = UK::getstr($content['diaryComment']['summary'], 1000, '' , 1);
                }
                break;
            case 4:         //文章
                $article = $content['article'];
                $articleInfo = $this->articleListModel->getArticleInfoByArticleBodyId($article['articleBodyId'], $article['authorUid']);
                if(empty($articleInfo)){
                    AC::$errorNo    =   'IC005';
                    AC::$errorMsg   =   '内容不存在或已被删除';
                    return  FALSE;
                }
                $content['article']['summary'] = UK::getstr($content['article']['summary'], 1000, '' , 1);
                break;
            case 5:         //匿名
                $anonymous = $content['anonymous'];
                $content['anonymous']['summary'] = UK::getstr($content['anonymous']['summary'], 1000, '' , 1);
            case 6:         //匿名评论
                $anonymous = isset($anonymous) ? $anonymous : $content['anonymousComment'];
                if(empty($anonymous)){
                    AC::$errorNo    =   'IC004';
                    AC::$errorMsg   =   '举报内容不能为空';
                    return  FALSE;
                }
                if ( ($anonymous['bodyId'] % 1000) >31){
                    AC::$errorNo    =   'IC001';
                    return  FALSE;
                }
                $anonymousInfo = $this->anonymousModel->getAnonymousInfoByBodyId($anonymous['bodyId']);
                if (AC::$errorNo || empty($anonymousInfo)){
                    AC::$errorNo    =   'IC005';
                    AC::$errorMsg   =   '内容不存在或已被删除';
                    return  FALSE;
                }
                if(isset($content['anonymousComment'])){
                    if(isset($diary['positionId']) && !empty($diary['positionId'])){
                        //评论楼中楼
                        $commentInfo = $this->childCommentModel->getCommentInfo(AC::COMMENT_TYPE_DIARY, $param['bodyId'], $diary['positionId']);
                    }else{
                        $commentInfo = $this->anonymousModel->getCommentInfoByBodyIdPos($param['bodyId'], $param['position']);
                    }
                    if (empty($commentInfo) || $commentInfo['status'] == 1){
                        AC::$errorNo    =   'IC005';//评论不存在
                        AC::$errorMsg   =   '内容不存在或已被删除';
                        return  false;
                    }
                    $content['anonymousComment']['summary'] = UK::getstr($content['anonymousComment']['summary'], 1000, '' , 1);
                }
                $this->rUid = $anonymousInfo['uid'];
                $this->bodyId = $anonymous['bodyId'];
                break;
            case 7:         // 圈子话题
                $group = $content['group'];
                $content['group']['summary'] = UK::getstr($content['group']['summary'], 1000, '' , 1);
            case 8:         //圈子话题评论
                $group = isset($group) ? $group : $content['groupComment'];
                $groupInfo = $this->topicModel->getTopicInfo(['tid' => $group['tid']]);
                if (AC::$errorNo || empty($groupInfo)){
                    AC::$errorNo    =   'IC005';
                    AC::$errorMsg   =   '内容不存在或已被删除';
                    return  FALSE;
                }
                if(isset($content['groupComment'])) {
                    //验证评论是否存在
                    if(isset($group['positionId']) && !empty($group['positionId'])){
                        //评论楼中楼
                        $commentInfo = $this->childCommentModel->getCommentInfo(AC::COMMENT_TYPE_GROUP, $group['tid'], $group['positionId']);
                    }else{
                        $info = [
                            'position'  =>  $group['position']+1,
                            'tid'       =>  $group['tid'],
                            'author_uid'=>  $group['authorUid'],
                        ];
                        $commentInfo = $this->topicCommentsModel->getCommentUid($info);
                    }
                    if(empty($commentInfo) || $commentInfo['status'] == 1){
                        AC::$errorNo    =   'IC005';
                        AC::$errorMsg   =   '内容不存在或已被删除';
                        return  FALSE;
                    }
                    $content['groupComment']['summary'] = UK::getstr($content['groupComment']['summary'], 1000, '' , 1);
                }
                break;
            case 9:         //举报用户
                $informedUserInfo   =   $this->autoUidModel->getUserBaseInfo(array('uid' => $content['user']['uid']));
                if (empty($informedUserInfo['account']) ||  empty($informedUserInfo['nickname']))
                {
                    AC::$errorNo    =   'IC005';
                    AC::$errorMsg   =   '用户不存在或账户异常';
                    return  FALSE;
                }
                break;
            case 10:        //圈子
                $groupInfo = $this->groupInfoModel->groupAddRedis($content['groupInfo']['gid']);
                if(empty($groupInfo)) {
                    AC::$errorNo    =   'IC005';
                    AC::$errorMsg   =   '内容不存在或账户异常';
                    return  FALSE;
                }
                $content['groupInfo']['summary'] = UK::getstr($content['groupInfo']['summary'], 1000, '' , 1);
                break;
            default:
                AC::$errorNo = 'IC006';
                AC::$errorMsg = '举报内容类型错误';
                return FALSE;
        }

       return $content;
    }

}


